<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.js"></script>
    <script src="https://unpkg.com/vue-router/dist/vue-router.js"></script>
    <style>
        .box {
            width: 80%;
            margin: 10px auto;
            min-height: 320px;
            border: 1px darkgoldenrod solid;
            border-radius: 10px;
        }

        .router-link-exact-active,
        .router-link-active {
            background-color: darkgoldenrod;
            color: black;
        }
    </style>
</head>

<body>
    <div id="app">
        <h1>{{title}}</h1>

        <hr>
        <!-- 5.路由出口 在哪里显示 路由组件 -->
        <div class="box">
            <router-view></router-view>
        </div>

        <!-- 6.路由导航 声明式导航 -->

    </div>

    <template id="App">
        <div>
            <h2>根组件 - 主视图 - App</h2>
            <mhead></mhead>
            <hr>
            <!-- 加载子路由  嵌套路由必须写-->
            <router-view></router-view>
            <my-foot></my-foot>
        </div>
    </template>

    <template id="home">
        <div>
            <h2>Home - Home - 首页</h2>
        </div>
    </template>

    <template id="news">
        <div>
            <h2>News - News - 新闻</h2>
        </div>
    </template>

    <template id="record">
        <div>
            <h2>Record - Record - 推荐记录</h2>
        </div>
    </template>

    <template id="mine">
        <div>
            <h2>Mine - Mine - 个人中心</h2>
        </div>
    </template>

    <template id="foot">
        <footer>
            <router-link :to="{name:l.name}" v-for="(l,i) in list" :key="i">
                {{l.text}}
            </router-link>
        </footer>
    </template>


    <script>

        Vue.component("mhead", {
            template: "<header>wh2105--yyds</header>"
        })
        // 0.如何使用模块化机制编程 ,导入Vue和VueRouter,要调用Vue.use(VueRouter)

        // 1.定义(路由)组件
        const App = {
            template: "#App",

        }
        const Home = {
            template: "#home"
        }
        const News = {
            template: "#news"
        }
        const Record = {
            template: "#record"
        }
        const Mine = {
            template: "#mine"
        }
        const myFoot = {
            template: "#foot",
            data() {
                return {
                    list: [/* 子路由 */
                        {
                            path: "home",
                            name: "home",
                            text: "首页"
                        },
                        {
                            path: "news",
                            name: "news",
                            text: "新闻"
                        },
                        {
                            path: "record",
                            name: "record",
                            text: "记录"
                        },
                        {
                            path: "mine",
                            name: "mine",
                            text: "个人中心"
                        },
                    ]
                }
            }
        }
        Vue.component("myFoot", myFoot)

        // 2.定义路由 定义好路由的匹配规则
        // 每个路由都应该隐射一个组件
        const routes = [
            { path: "/", component: App },
            {
                path: '/app',
                component: App,
                name: 'app',
                redirect: "/app/home",
                children: [ /* 声明子路由 */
                    {
                        path: "home",
                        name: "home",
                        component: Home
                    },
                    {
                        path: "news",
                        name: "news",
                        component: News
                    },
                    {
                        path: "record",
                        name: "record",
                        component: Record
                    },
                    {
                        path: "mine",
                        name: "mine",
                        component: Mine
                    },
                    {
                        path: "*",
                        redirect: {
                            name: "home"
                        }
                    }
                ]
            },
            {
                path: "*", redirect: {
                    name: "app"
                }
            }

        ]

        // 3.创建路由实例对象
        const router = new VueRouter({
            routes
        })

        // 4.挂载到根实例
        const vm = new Vue({
            router,
            watch: {
                '$route': function (to, from) {
                    console.log(to);
                    console.log(from);
                }
            },
            data: {
                title: "Vue Router 入门学习 -- 路由跳转 嵌套路由",
            },
            mounted() {
                console.log(this.$router);
                console.log(this.$route);

            }
        }).$mount("#app")
    </script>
</body>

</html>